<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="1100px" 
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    
    <el-form :model="Model" :rules="dialogProps.action === 'add' ? addRules : editRules" 
      ref="from" label-width="80px" label-position="right" size="small">
        <el-row>
            <el-col :span="5">
                <el-form-item label="身份证" prop="idCard">
                    <el-input :maxlength="32" v-model="Model.idCard"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="5">
                <el-form-item label="档案号" prop="fileNumber">
                    <el-input :maxlength="32" v-model="Model.fileNumber"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="5">
                <el-form-item label="姓名" prop="memberName">
                    <el-input :maxlength="32" v-model="Model.memberName"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="5" style="text-align: right;">
                <el-button type="primary" size="small" @click="findMember">查询</el-button>
            </el-col>
        </el-row>
      <el-tabs type="border-card" v-model="activeTabName">
        <el-tab-pane v-for="(item1,index1) in Model.questionnaireAnswer" :key="item1.id" :name="'wj'+index1" :label="'问卷'+index1" class="user-info-tab-pane">
          根据一年内的感觉回答以下问题
          <el-row v-for="(item2) in item1" :key="item2.id">
              <el-col>
                {{item2.problem}}
                <el-form-item>
                  <el-radio-group v-model="item2.answer">
                    <el-radio label="没有（根本不）"></el-radio>
                    <el-radio label="很少（有一点）"></el-radio>
                    <el-radio label="有时（有些）"></el-radio>
                    <el-radio label="经常（相当）"></el-radio>
                    <el-radio label="总是（非常）"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>     
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('from')">确 定</el-button>
    </span>    
  </el-dialog>
</template>


<script>
import { saveUser, updateUser } from '@/api/user'
import {updateEmp} from '@/api/emp'
import { addAssessResult,updateAssessResult,getAssessResult } from '@/api/assessResult'
import { addMember,updateMember,getMember,findMember } from '@/api/member'
export default {
  name: "user-edit",
  data() {
    // var validatePassword = (rule, value, callback) => {
    //   if (value === '' || value === undefined) {
    //     callback(new Error('请输入密码'));
    //   } else {
    //     if (this.Model.checkPassword !== '') {
    //       this.$refs.from.validateField('checkPassword');
    //     }
    //     callback()
    //   }
    // }
    // var validateCheckPassword = (rule, value, callback) => {
    //   if (value === '' || value === undefined) {
    //     callback(new Error('请再次输入密码'))
    //   } else if (value !== this.Model.password) {
    //     callback(new Error('两次输入密码不一致!'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loading: false,
      Model: {
        id:0,
        type:1,
        idCard:'',
        fileNumber:'',
        memberName:'',
        questionnaireAnswer:[[{problem:"头痛",answer:""},
          {problem:"神经过敏，心中不踏实",answer:""},
          {problem:"头脑中有不必要的想法",answer:""},
          {problem:"头晕和昏倒",answer:""},
          {problem:"对异性兴趣减退",answer:""},
          {problem:"对旁人责备求全",answer:""},
          {problem:"感到别人能控制你的想法",answer:""},
          {problem:"责怪别人制造麻烦",answer:""},
          {problem:"忘记性大",answer:""},
          {problem:"担心自己的服饰整齐及仪态端正",answer:""},
          {problem:"胸痛",answer:""},
          {problem:"害怕空旷的场所或街道",answer:""},
          {problem:"感到自己的精力下降，活动减慢",answer:""},
          {problem:"想结束自己的生命",answer:""},
          {problem:"听到旁人听不到的声音",answer:""},
          {problem:"发抖",answer:""},
          {problem:"感到大多数人不可信任",answer:""},
          {problem:"胃口不好",answer:""}]]
      },
      roleCheckboxList: {
        checkAll: false,
        isIndeterminate: true,
        roles: []
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      activeTabName: 'wj0',
      addRules: {
        
      },
      editRules: {
        
      }
    };
  },
  methods: {
    findMember(){
        findMember(this.Model.idCard).then(response => {
          console.log(response.data)
          this.Model.fileNumber=response.data.fileNumber
          this.Model.memberName=response.data.name
        })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveUser()
          } else {
            this.doUpdateUser()
          }
          
        } else {
          return false
        }
      })
      
    },
    doUpdateUser() {
      this.loading = true
      console.log(this.Model)
      updateAssessResult(this.Model)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.clearFrom()
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)      
      })
      
    },
    doSaveUser() {
      this.loading = true
      console.log(this.Model)
      addAssessResult(this.Model)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.clearFrom()
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error) 
      })
      
    },    
    clearFrom(){
      for(let key in this.Model){
        this.Model[key]  = ''
        if(key=='status'){
          this.Model[key]  = '启用'
        }
      }
    },
    handleDialogClose() {
      this.dialogProps.visible = false  
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['from'].clearValidate()
        // this.$refs['focus'].focus()
      })
    },
    getAllRoleId() {
      let allRoleId = []
      for(let role of this.roleCheckboxList.roles) {
        allRoleId.push(role.id)
      }
      return allRoleId
    },
    handleCheckAllChange(val) {
      this.Model.roleIds = (val ? this.getAllRoleId() : [])
      this.roleCheckboxList.isIndeterminate = false
    },
    handleCheckedRolesChange(value) {
      let checkedCount = value.length
      this.roleCheckboxList.checkAll = checkedCount === this.roleCheckboxList.roles.length
      this.roleCheckboxList.isIndeterminate = checkedCount > 0 && checkedCount < this.roleCheckboxList.roles.length
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })      
    }
  },
  mounted: function() {
    this.$nextTick(() => {  
      this.$on('openEditUserDialog', function(model) {
        this.Model = model
        console.log(model.questionnaireAnswer)
        this.Model.questionnaireAnswer= JSON.parse(model.questionnaireAnswer)
        // this.roleCheckboxList.roles = roles        
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改评估'
        this.activeTabName = 'wj0'
        this.dialogProps.visible = true
      })
      this.$on('openAddUserDialog', function(type) {
        // this.Model = {
        //   roleIds: []
        // }
        // this.roleCheckboxList.roles = roles
        this.Model.type=type
        console.log(type)
        if(type==1){
          this.Model.questionnaireAnswer=[[{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}],
          [{problem:"您手脚发凉吗？",answer:""},{problem:"您胃脘部、背部或腰膝部怕冷吗？",answer:""},{problem:"您感到怕冷、衣服比别人穿得多吗？",answer:""},{problem:"您比一般人受不了寒冷（冬天的寒冷，夏天的冷空调、电扇等）。",answer:""},{problem:"您比别人容易患感冒吗？",answer:""},{problem:"您吃（喝）凉的东西会感到不舒服或者怕吃（喝）凉东西吗？",answer:""},{problem:".你受凉或吃（喝）凉的东西后，容易腹泻（拉肚子）吗？",answer:""}]]
        }
        if(type==2){
          this.Model.questionnaireAnswer=[[{problem:"头痛",answer:""},
          {problem:"神经过敏，心中不踏实",answer:""},
          {problem:"头脑中有不必要的想法",answer:""},
          {problem:"头晕和昏倒",answer:""},
          {problem:"对异性兴趣减退",answer:""},
          {problem:"对旁人责备求全",answer:""},
          {problem:"感到别人能控制你的想法",answer:""},
          {problem:"责怪别人制造麻烦",answer:""},
          {problem:"忘记性大",answer:""},
          {problem:"担心自己的服饰整齐及仪态端正",answer:""},
          {problem:"胸痛",answer:""},
          {problem:"害怕空旷的场所或街道",answer:""},
          {problem:"感到自己的精力下降，活动减慢",answer:""},
          {problem:"想结束自己的生命",answer:""},
          {problem:"听到旁人听不到的声音",answer:""},
          {problem:"发抖",answer:""},
          {problem:"感到大多数人不可信任",answer:""},
          {problem:"胃口不好",answer:""}]]
        }
        if(type==3){
          
        }
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加评估'
        this.activeTabName = 'wj0'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

<style lang="scss" scoped>
  .user-info-tab-pane {
    margin-top: 10px;
  }
</style>





